<template>
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" >
    <el-form-item prop="email" >
      <el-input type="text" v-model="ruleForm.email" autocomplete="off" placeholder="请输入邮箱" clearable ></el-input>
    </el-form-item>
    <el-form-item prop="password" >
      <el-input type="password" v-model="ruleForm.password" autocomplete="off" placeholder="请输入密码" show-password></el-input>
    </el-form-item>
    <el-form-item prop="checkPassword" >
      <el-input type="password" v-model="ruleForm.checkPassword" autocomplete="off" placeholder="请再次输入密码" show-password></el-input>
    </el-form-item>
    <el-form-item prop="agreed" style="margin-bottom: 5px">
      <el-checkbox v-model="ruleForm.agreed"></el-checkbox>
      <span style="margin-left: 5px;color: gray">已阅读并同意小米帐号</span><a href="" style="color: black"> 用户协议 </a>和 <a href="" style="color: #333333">隐私政策</a>
      <!--      <br>-->
      <!--      <span v-if="!agreed">不同意隐私政策还想登陆？做梦！</span>-->
    </el-form-item>
    <el-form-item >
      <el-button type="primary" @click="submitForm('ruleForm')" style="width: 100%;background-color: coral;border-color: coral">注 册</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { validateEmail } from "@/utils/formValidator";
import { register } from "@/api/login";
import { removeToken } from "@/utils/auth";

export default {
  name: "registerForm",
  data() {
    //检验密码是否符合要求
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('*请输入密码'));
      } else if (value.trim().length < 8) {
        callback(new Error('*密码不得少于8位'))
      } else {
        if (this.ruleForm.checkPassword !== '') {
          this.$refs.ruleForm.validateField('checkPassword');
        }
        callback();
      }
    };

// 检验二次输入的密码是否一致
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        email: "",
        password: "",
        checkPassword: ""
      },
      rules: {
        email: [
          { required: true, validator: validateEmail, trigger: 'blur'},
        ],
        password: [
          { required: true, validator: validatePass, trigger: 'blur'}
        ],
        checkPassword: [
          {validator: validatePass2, trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('submit!');
          if (this.ruleForm.agreed) {
            let form = this.ruleForm
            register(form.email,form.password).then(() => {
              alert("注册成功，即将跳转至登陆页面")
              removeToken()
              location.href = '/login';
            }).catch(error => {
              this.reject(error)
            })
          } else {
            alert('*需要先同意隐私条款才可注册！')
            return false;
          }
        } else {
          // console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>
.el-form {
  padding-top: 20px;
}
a {
  text-decoration: none;
  color: #ff5c00;
}
/deep/ .el-input__inner{
  height: 60px;
  width: 100%;
}
</style>
